package com.egao.common.module.pushInformation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.module.pushInformation.entity.PushInformationPosition;
import com.egao.common.module.pushInformation.entity.PushInformationPositionResult;
import com.egao.common.module.pushInformation.mapper.PushInformationPositionResultMapper;
import com.egao.common.module.pushInformation.service.PushInformationPositionResultService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author lym
 * @date 2024/8/23 11:14
 */
@Service
public class PushInformationPositionResultServiceImpl extends ServiceImpl<PushInformationPositionResultMapper, PushInformationPositionResult> implements PushInformationPositionResultService {

    @Override
    public List<PushInformationPositionResult> computeResult(List<PushInformationPosition> pushInformationPositionList) {
        // 对职位分组
        Map<String, List<PushInformationPosition>> map = pushInformationPositionList.stream().collect(Collectors.groupingBy(PushInformationPosition::getPositionName));

        List<PushInformationPositionResult> resultList = new ArrayList<>();
        map.forEach((key, value) -> {
            int count100 = 0;
            int count95 = 0;
            int count90 = 0;
            int count85 = 0;
            Integer positionId = null;
            for (PushInformationPosition item : value) {
                positionId = item.getPositionId();
                if (item.getMatching() == 100) {
                    count100++;
                }
                if (item.getMatching() >= 95) {
                    count95++;
                }
                if (item.getMatching() >= 90) {
                    count90++;
                }
                if (item.getMatching() >= 85) {
                    count85++;
                }
            }

            if (count100 > 0) {
                PushInformationPositionResult result = new PushInformationPositionResult();
                result.setPositionId(positionId);
                result.setPositionName(key);
                result.setMatchingNumber(count100);
                result.setMatching(100);
                resultList.add(result);
            }

            if (count95 > 0) {
                // 如果没有100的显示95的
                if (count100 == 0) {
                    PushInformationPositionResult result = new PushInformationPositionResult();
                    result.setPositionId(positionId);
                    result.setPositionName(key);
                    result.setMatchingNumber(count95);
                    result.setMatching(95);
                    resultList.add(result);
                }
            }

            if (count90 > 0) {
                PushInformationPositionResult result = new PushInformationPositionResult();
                result.setPositionId(positionId);
                result.setPositionName(key);
                result.setMatchingNumber(count90);
                result.setMatching(90);
                resultList.add(result);
            }

            if (count85 > 0) {
                PushInformationPositionResult result = new PushInformationPositionResult();
                result.setPositionId(positionId);
                result.setPositionName(key);
                result.setMatchingNumber(count85);
                result.setMatching(85);
                resultList.add(result);
            }
        });

        return resultList;
    }

}
